{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Setting graph specification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "global graph_opts note(, justification(left) color(black) span pos(7)) title(, justification(left) color(black) span pos(11)) subtitle(, justification(left) color(black) span pos(11)) graphregion(color(white)) ylab(,angle(0) nogrid) ytit(\"\") xtit(,placement(left) justification(left)) yscale(noline) xscale(noline) xsize(7) legend(region(lc(none) fc(none)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Running ado.files\n",
    "``betterbar`` produces bar graphs with grouping of variables, standard error bars, and cross-group comparisons.\n",
    "\n",
    "``labelcollapse`` preserves variable labeling while performing ``collapse``."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "qui do \"https://raw.githubusercontent.com/worldbank/Stata-IE-Visual-Library/develop/Library/Bar%20plots/Side%20by%20side%20horizontal%20bar%20plot%20(betterbar)/betterbar.ado\"\n",
    "qui do \"https://raw.githubusercontent.com/worldbank/Stata-IE-Visual-Library/develop/Library/Bar%20plots/Side%20by%20side%20horizontal%20bar%20plot%20(betterbar)/labelcollapse.ado\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Creating locals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "local n_5 = 599\n",
    "local n_6 = 601\n",
    "\n",
    "local title_5 = \"Classic case of presumed TB\"\n",
    "local title_6 = \"TB case with positive sputum report\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Creating the graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "qui forvalues i = 5/6 {\n",
    "\n",
    "\tlocal case = `i' - 4\n",
    "\n",
    "\tuse \"https://github.com/worldbank/Stata-IE-Visual-Library/raw/develop/Library/Bar%20plots/Side%20by%20side%20horizontal%20bar%20plot%20(betterbar)/data.dta\" , clear\n",
    "\n",
    "\t\tgen n = 1\n",
    "\t\tbys med_generic: egen med_class_typ = mode(med_class), minmode // Label with most typical medicine code\n",
    "\t\t\tlabel val med_class_typ med_k\n",
    "\n",
    "\t\tkeep if case == `i'\n",
    "\n",
    "\t\tlabelcollapse (firstnm) n med_class_typ med_generic_encoded sp_location, by(med_generic facilitycode) vallab(med_class_typ med_generic_encoded sp_location)\n",
    "\n",
    "\t\tlabelcollapse (sum) n (firstnm) med_generic_encoded med_class_typ, by(med_generic) vallab(med_class_typ med_generic_encoded) \n",
    "\n",
    "\t\tcap separate n, by(med_generic_encoded) shortlabel\n",
    "\t\t\tforeach var of varlist n?* {\n",
    "\t\t\t\tlocal theLabel : var label `var'\n",
    "\t\t\t\tlocal theLabel = regexr(\"`theLabel'\",\"med_generic_encoded == \",\"\")\n",
    "\n",
    "\t\t\t\tcap su n if med_generic == \"`theLabel'\"\n",
    "\t\t\t\t\tcap local theN = `r(mean)'\n",
    "\n",
    "\t\t\t\tlabel var `var' \"`theLabel' [`theN']\"\n",
    "\t\t\t\t}\n",
    "\n",
    "\t\t\tforeach var of varlist n?* {\n",
    "\t\t\t\treplace `var' = . if `var' < 5 // Exclude low volumes\n",
    "\t\t\t\treplace `var' = `var'/`n_`i'' // Number of interactions\n",
    "\t\t\t\tqui sum `var'\n",
    "\t\t\t\t\tif `r(N)' == 0 drop `var' \n",
    "\t\t\t\t}\n",
    "\n",
    "\t\tdrop if med_generic == \"Sodium Chloride\" // not an active ingredient\n",
    "\t\t\n",
    "\t\tbetterbar (n?*) , stat(sum) over(med_class_typ) by(med_class_typ) nobylabel nobycolor d(1)  ///\n",
    "\t\t\tlegend(span c(1) pos(3) ring(1) symxsize(small) symysize(small) size(small))  ///\n",
    "\t\t\tdropzero ///\n",
    "\t\t\txlab(0 \"0%\" .2 \"20%\" .4 \"40%\" .6 \"60%\") ysize(6) labsize(2) $graph_opts title(\"Case `case' (N=`n_`i'')\") subtitle(\"`title_`i''\")\n",
    "\n",
    "\t\t* graph export \"generics_sp`i'.png\", replace width(4000)\n",
    "\t\t\tgraph save \"figure_4_`case'.gph\" , replace\n",
    "\t\t\n",
    "\t}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. Combine multiple graphs into one with a single common legend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "grc1leg ///\n",
    "\t\"figure_4_1.gph\" ///\n",
    "\t\"figure_4_2.gph\" ///\n",
    "\t, pos(3) graphregion(color(white)) xsize(7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. Exporting the graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "qui graph export \"figure.png\", replace width(2000)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Stata",
   "language": "stata",
   "name": "stata"
  },
  "language_info": {
   "file_extension": ".do",
   "mimetype": "text/x-stata",
   "name": "stata"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
